#include "mychartview.h"
#include <QApplication>
#include <QValueAxis>
#include <QDateTimeAxis>
#include <QDateTime>
MyChartView::MyChartView(QWidget *parent)
    : QChartView{ parent }
    , prevPoint_{}
    , leftButtonPressed_{}
    , ctrlPressed_{}
    , alreadySaveRange_{}
    , xRange_{}
    , yRange_{}
    , coordItem_{}
{
    qDebug()<<"alreadySaveRange_"<<alreadySaveRange_;
}

MyChartView::MyChartView(QChart *chart, QWidget *parent)
    :QChartView(parent)
{
    setChart(chart);
}

MyChartView::~MyChartView()
{

}

void MyChartView::saveAxisRange()
{
    const QValueAxis* axisY = qobject_cast<QValueAxis*>(chart()->axisY());
    yRange_ = { axisY->min(), axisY->max() };

    const QDateTimeAxis* axisX = qobject_cast<QDateTimeAxis*>(chart()->axisX());
    xRange_.clear();
    xRange_.push_back(axisX->min());
    xRange_.push_back(axisX->max());
}

void MyChartView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        prevPoint_ = event->pos();
        leftButtonPressed_ = true;
    }
}

void MyChartView::mouseMoveEvent(QMouseEvent *event)
{
    if (!coordItem_) {
        coordItem_ = new QGraphicsSimpleTextItem{ chart() };
        coordItem_->setZValue(5);
        coordItem_->setPos(100, 60);
        coordItem_->show();
    }
    const QPoint curPos{ event->pos() };
    const QPointF curVal{ chart()->mapToValue(QPointF(curPos)) };
    coordItem_->setText(QString("X = %1, Y = %2").arg(QDateTime::fromMSecsSinceEpoch(curVal.x()).toString("yyyy/MM/dd HH:mm:ss")).arg(curVal.y()));

    if (leftButtonPressed_) {
        const auto offset = curPos - prevPoint_;
        prevPoint_ = curPos;
        if (!alreadySaveRange_) {
            saveAxisRange();
            alreadySaveRange_ = true;
        }
        chart()->scroll(-offset.x(), offset.y());
    }
}

void MyChartView::mouseReleaseEvent(QMouseEvent *event)
{
    leftButtonPressed_ = false;
    if (event->button() == Qt::RightButton) {
        if (alreadySaveRange_) {
            chart()->axisX()->setRange(xRange_[0], xRange_[1]);
            chart()->axisY()->setRange(yRange_[0], yRange_[1]);
        }
    }
}

void MyChartView::wheelEvent(QWheelEvent *event)
{
#if (QT_VERSION <= QT_VERSION_CHECK(6,0,0))
    const auto pos  = QPointF(event->pos());
    const auto isZoomIn = event->delta() > 0;
#else
    const auto pos  = event->position();
    const auto isZoomIn = event->angleDelta().y() > 0;
#endif
    const QPointF curVal = chart()->mapToValue(pos);

    if (!alreadySaveRange_) {
        saveAxisRange();
        alreadySaveRange_ = true;
    }

    if (ctrlPressed_) {
        QValueAxis* axis = qobject_cast<QValueAxis*>(chart()->axisY());
        double scaling = 1.5;

        const double down = axis->min();
        const double up = axis->max();

        double downOffset{};
        double upOffset{};
        if (isZoomIn) {
            downOffset = (curVal.y() - down) / scaling;
            upOffset = (up - curVal.y()) / scaling;
        }
        else {
            downOffset = (curVal.y() - down) * scaling;
            upOffset = (up - curVal.y()) * scaling;
        }

        axis->setRange(curVal.y() - downOffset, curVal.y() + upOffset);

        //zoomValue(axis, curVal.y(), isZoomIn);
    }else {
        QDateTimeAxis* axis = qobject_cast<QDateTimeAxis*>(chart()->axisX());
        double scaling = 1.5;

        const QDateTime left = axis->min();
        const QDateTime right = axis->max();

        qreal leftoffset{};
        qreal rightoffset{};

        if (isZoomIn) { //放大
            leftoffset = (curVal.x() - left.toMSecsSinceEpoch()) / scaling;
            rightoffset = (right.toMSecsSinceEpoch() - curVal.x()) / scaling;
        }
        else {
            leftoffset = (curVal.x() - left.toMSecsSinceEpoch()) * scaling;
            rightoffset = (right.toMSecsSinceEpoch() - curVal.x()) * scaling;
        }

        axis->setRange(QDateTime::fromMSecsSinceEpoch(curVal.x() - leftoffset), QDateTime::fromMSecsSinceEpoch(curVal.x() + rightoffset));

    }
}

void MyChartView::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Control)
        ctrlPressed_ = true;
}

void MyChartView::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Control)
        ctrlPressed_ = false;
}
